<template>
  <div>
    <SearchForm @search="getAllParams" v-if="$permiss[969]" :showReset="false">
      <el-col :span="8" :xs="24">
        <el-form-item :label="$t('所属租户')">
          <el-select style="width: 100%" v-model="page.adminUserId">
            <el-option v-for="item in tenantList" :key="item.adminUserId" :label="item.adminName"
                       :value="item.adminUserId"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
    </SearchForm>
    <el-card shadow="never" style="margin-top: 10px">
      <el-card class="box-card" shadow="hover" v-for="item in paramsList" :key="item.id">
        <div slot="header" class="clearfix">
          <span>{{ configTitle[item.configKey] }}</span>
        </div>
        <div style="overflow: auto">
          <!--                罚息设置-->
          <div v-if="item.configKey === '1'">
            <el-input v-model="item.configValue" oninput="value = value.replace(/[^\d]/g, '')">
              <template slot="prepend"> {{ $t('罚息达到合同金额的') }}</template>
              <template slot="append">{{ $t('%后，将不再继续叠加计算') }}</template>
            </el-input>
          </div>
          <!--          减免配置-->
          <div v-if="item.configKey === '2'">
            <el-input v-model="item.configValue" oninput="value = value.replace(/[^\d]/g, '');if(value>=100)value=100">
              <template slot="prepend"> {{ $t('申请减免时，减免金额在当前订单罚息的') }}</template>
              <template slot="append">{{ '%' + " " + $t('范围内可直接通过，免去财务审核流程') }}</template>
            </el-input>
          </div>
          <!--          打款笔数设置-->
          <div v-if="item.configKey === 'pay_num'">
            <el-input v-model="item.configValue" style="width: 150px" oninput="value = value.replace(/[^\d]/g, '');">
              <template slot="prepend"> {{ $t('新客限制每日线上打款笔数为') }}</template>
            </el-input>

            <el-alert
                style="margin-top: 15px;width:400px"
                :title="$t('设为零表示无限制，溢出线上退款不受限')"
                type="info"
                :closable="false"
                show-icon>
            </el-alert>
          </div>
          <!--          打款设置-->
          <div v-if="item.configKey === 'DAY_ID_PAY_NUM'">
            <el-input v-model="item.configValue" style="width: 150px" oninput="value = value.replace(/[^\d]/g, '');">
              <template slot="prepend"> {{ $t('同一身份证一天内打款') }}</template>
              <template slot="append">{{ $t('笔') }}</template>
            </el-input>
          </div>
          <!-- 营销分单回收设置-->
          <div v-if="item.configKey === 'RESET_MARKETING_DATA_DAY'">
            <el-input v-model="item.configValue">
              <template slot="prepend"> {{ $t('营销分单后') }}</template>
              <template slot="append">{{ $t('天未申请，系统将自动回收') }}</template>
            </el-input>
          </div>
          <!-- AI电话设置-->
          <div v-if="item.configKey === 'VOICE_AI_SWITCH'">
          <span>
            <span style="margin-right: 5px">订单满足贷中贷后催收条件，系统自动拨打AI电话</span>
            <el-switch v-model="item.configValue" active-value="1" inactive-value="0"/>
          </span>
          </div>
          <!-- 白名单有效期设置-->
          <div v-if="item.configKey === 'white_list_valid_day'">
            <el-input v-model="item.configValue" oninput="value = value.replace(/[^\d]/g, '');if(value<=0)value=''">
              <template slot="prepend">白名单自加入时间开始</template>
              <template slot="append"> 天内有效，过期则视为非白名单</template>
            </el-input>
          </div>
        </div>
      </el-card>
      <el-card class="box-card" shadow="hover">
        <div slot="header" class="clearfix">
          <span>{{ $t('共债设置') }}</span>
        </div>
        <div style="overflow: auto">
          <el-button type="primary" icon="el-icon-plus" @click="addCodebtSet">{{ $t('新增共债设置') }}</el-button>
          <el-form inline style="margin-top: 15px" v-for="(item,index) in codebtConfigList" :key="index">
            <el-form-item :label="$t('客户累计结清次数')" required>
              <div style="display:flex;">
                <el-input v-model="item.configValue" oninput="value = value.replace(/[^\d]/g, '')"/>
                <div style="text-align: center;line-height: 32px;padding: 0 10px">-</div>
                <el-input v-model="item.configValueTwo" oninput="value = value.replace(/[^\d]/g, '')"/>
              </div>
            </el-form-item>
            <el-form-item :label="$t('允许最大订单数')" required>
              <el-input v-model="item.configValueThree" oninput="value = value.replace(/[^\d]/g, '')"/>
            </el-form-item>
            <el-button @click="delCodebt(index,item)" icon="el-icon-close"></el-button>
          </el-form>
        </div>
      </el-card>
      <div style="margin-top: 15px;text-align: center" v-if="$permiss[968]">
        <el-button type="primary" style="margin-right: 50px" @click="paramsConfirm">{{ $t('确认') }}</el-button>
        <el-button @click="getAllParams">{{ $t('重置') }}</el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
import {selectConfig, deleteConfig, updateConfig, selectTenant} from '@/api/system'

export default {
  data() {
    return {
      page: {},
      tenantList: [],
      paramsList: [],
      configTitle: {
        '1': this.$t('罚息设置'),
        '2': this.$t('减免设置'),
        'pay_num': this.$t('打款设置'),
        'RESET_MARKETING_DATA_DAY': '营销分单回收设置',
        'VOICE_AI_SWITCH': 'AI电话设置',
        'DAY_ID_PAY_NUM': '打款笔数设置',
        'white_list_valid_day':'白名单有效期设置'
      },
      codebtConfigList: []
    }
  },
  mounted() {
    this.getSelect()
  },
  methods: {
    async getSelect() {
      this.tenantList = await selectTenant().then(res => res.data)
      if (this.tenantList.length) {
        this.$set(this.page, 'adminUserId', this.tenantList[0].adminUserId)
      }
      await this.getAllParams()
    },
    // 获取所有参数配置
    async getAllParams() {
      const res = await selectConfig({adminUserId: this.page.adminUserId})
      if (res.code === 1) {
        this.paramsList = res.data.filter(item => item.configKey !== '3')
        this.codebtConfigList = res.data.filter(item => item.configKey === '3')
      }
    },
    // 新增共债设置
    addCodebtSet() {
      this.codebtConfigList.push({
        configKey: '3',
      })
    },
    delCodebt(index, {id}) {
      if (!id) {
        this.codebtConfigList.splice(index, 1)
      } else {
        this.$confirm(this.$t('确认删除') + '?', this.$t('提示'), {
          type: 'warning'
        }).then(async () => {
          const res = await deleteConfig({id})
          if (res.code === 1) {
            this.$message.success(this.$t('删除成功'))
            this.getAllParams()
          }
        })
      }
    },
    // 参数配置提交
    async paramsConfirm() {
      if (this.codebtConfigList.length > 0) {
        for (let i = 0; this.codebtConfigList.length > i; i++) {
          const item = this.codebtConfigList[i]
          if (!item['configValue'] || !item['configValueTwo'] || !item['configValueThree']) {
            this.$message.error(this.$t('共债设置不能为空'))
            return
          }
        }
      }
      const list = [...this.paramsList, ...this.codebtConfigList,]
      const res = await updateConfig(list)
      if (res.code === 1) {
        this.$message.success(this.$t('操作成功'))
        this.getAllParams()
      }
    }
  },

}
</script>

<style lang="less" scoped>
.box-card {
  margin-bottom: 10px;

  ::v-deep .el-input-group {
    width: 0% !important;
  }

  ::v-deep .el-input__inner {
    width: 100px !important;
  }
}

</style>
